//
//  MainViewController.m
//  V3d_Cocoa
//
//  Created by Y my on 11-3-14.
//  Copyright 2011 NJUST. All rights reserved.
//

#import "MainViewController.h"
#import "GLViewController.h"


@implementation MainViewController

//@synthesize main_view;
@synthesize xy_view, yz_view, xz_view;
@synthesize z, x, y;
@synthesize x_slider, y_slider, z_slider;
@synthesize openfile, seeIn3d;
@synthesize seg_con;
@synthesize mainViewData, my4DImage;


/////////// ViewControllerDelegate ///////////////

- (void)viewDidLoad {	
	self.title = [NSString stringWithFormat:@"V3D_ipad"];
	
	UIGestureRecognizer *pinchgr;
	pinchgr = [[UIPinchGestureRecognizer alloc] initWithTarget:self.xy_view action:@selector(pinch:)];
	[self.xy_view addGestureRecognizer:pinchgr];
	[pinchgr release];
	
	pinchgr = [[UIPinchGestureRecognizer alloc] initWithTarget:self.xz_view action:@selector(pinch:)];
	[self.xz_view addGestureRecognizer:pinchgr];
	[pinchgr release];
	
	pinchgr = [[UIPinchGestureRecognizer alloc] initWithTarget:self.yz_view action:@selector(pinch:)];
	[self.yz_view addGestureRecognizer:pinchgr];
	[pinchgr release];
	
	
	UIGestureRecognizer *pangr;
	pangr = [[UIPanGestureRecognizer alloc] initWithTarget:self.xy_view action:@selector(pan:)];
	[self.xy_view addGestureRecognizer:pangr];
	[pangr release];
	
	pangr = [[UIPanGestureRecognizer alloc] initWithTarget:self.xz_view action:@selector(pan:)];
	[self.xz_view addGestureRecognizer:pangr];
	[pangr release];
	
	pangr = [[UIPanGestureRecognizer alloc] initWithTarget:self.yz_view action:@selector(pan:)];
	[self.yz_view addGestureRecognizer:pangr];
	[pangr release];
	
	[self updateViewsData];
}

- (void)viewDidUnload {
	[self.xy_view release];
	[self.yz_view release];
	[self.xz_view release];
}

- (UIView*)getView {
	return self.view;
}

////////////// init & dealloc //////////////////////////

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		//if (my4DImage) {
//			[my4DImage release];
//		}
//		my4DImage = [[My4DImage alloc] init];
		
		//if (mainViewData) {
//			[mainViewData release];
//		}
//		mainViewData = [[MainViewData alloc] init];
	}
	
	return self;
}

- (void)dealloc {
	[my4DImage release];
	[mainViewData release];
	[xy_view release];
	[yz_view release];
	[xz_view release];
	
	[super dealloc];
}

/////////////// UI functions //////////////////

- (IBAction)openFileButtonPressed:(id)sender {
	[self openFile];
	[self updateViewsData];
	[self setViewsFrame];
	[self.x_slider setValue:50.0];
	[self.y_slider setValue:50.0];
	[self.z_slider setValue:50.0];
	self.seg_con.selectedSegmentIndex = mainViewData.ctype;
	if(mainViewData.ctype == colorGray)
	{
		[seg_con setEnabled:NO forSegmentAtIndex:0];
		for(int i = 4; i < 8; i++)
			[seg_con setEnabled:NO forSegmentAtIndex:i];
	}
}

- (IBAction)seeIn3DButtonPressed:(id)sender {
	[self seeIn3D];
}

- (IBAction)radioButtonPressed:(id)sender{
	// NSLog(@"radioButton pressed");
	if( !my4DImage || !mainViewData)
		return;
	else {
		NSInteger index = [sender selectedSegmentIndex];
		if(index == colorRGB)
			mainViewData.ctype = colorRGB;
		else if(index == 1)
			mainViewData.ctype = colorGray;
		else if(index == 2)
			mainViewData.ctype = colorRedOnly;
		else if(index == 3)
			mainViewData.ctype = colorRed2Gray;
		else if(index == 4)
			mainViewData.ctype = colorGreenOnly;
		else if(index == 5)
			mainViewData.ctype = colorGreen2Gray;
		else if(index == 6)
			mainViewData.ctype = colorBlueOnly;
		else if(index == 7)
			mainViewData.ctype = colorBlue2Gray;
		}
	
}


- (IBAction)sliderX:(UISlider*)sender {
	CGFloat n = [sender value] / 100.0;
	if (n != self.mainViewData.curFocusXratio) {
		self.mainViewData.curFocusXratio = n;
	}
	
}

- (IBAction)sliderY:(UISlider*)sender {
	CGFloat n = [sender value] / 100.0;
	if (n != self.mainViewData.curFocusYratio) {
		self.mainViewData.curFocusYratio = n;
	}
}

- (IBAction)sliderZ:(UISlider*)sender {
	CGFloat n = [sender value] / 100.0;
	if (n != self.mainViewData.curFocusZratio) {
		self.mainViewData.curFocusZratio = n;
	}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
	return YES;
}

//////////////// main functions /////////////////

- (void)openFile {
	if (self.my4DImage) {
		[my4DImage release];
	}
	if (mainViewData) {
		[mainViewData release];
	}
	
	my4DImage = [[My4DImage alloc] init];
	mainViewData = [[MainViewData alloc] init];
	
	NSString* path = [[NSBundle mainBundle] bundlePath];
	NSString* tiffPath = [[path stringByDeletingLastPathComponent] stringByAppendingString:@"/tt2.tif"];
	//NSString* tifPath = [NSString stringWithFormat:@"%s
	[self.my4DImage loadImage:[tiffPath UTF8String]];
	self.mainViewData.bDrawFocusCrossLine = YES;
}

- (void)seeIn3D {
	//MyNavigationController *navCon = [MyNavigationController getInstance];  // this line will be modified in IOS development
	GLViewController *glViewCon = [[GLViewController alloc] initWithNibName:@"GLView" bundle:nil];
	[glViewCon setData:self.my4DImage];
	glViewCon.title = [NSString stringWithFormat:@"3D View"];
	[self.navigationController pushViewController:glViewCon animated:YES];
	[glViewCon release];
}

- (void)setViewsFrame {
	int x_pixels = CGImageGetWidth(self.xy_view.pixmap);
	int y_pixels = CGImageGetHeight(self.xy_view.pixmap);
	int z_pixels = CGImageGetWidth(self.yz_view.pixmap);
	
	if (x_pixels + z_pixels + 2 * SPACE_PIXELS < MAX_DESP_WIDTH_PIXELS) {
		CGFloat ratio = (CGFloat)(MAX_DESP_WIDTH_PIXELS - 2 * SPACE_PIXELS) / (CGFloat)(x_pixels + z_pixels);
		x_pixels *= ratio;
		y_pixels *= ratio;
		z_pixels *= ratio;
	}
	if (y_pixels + z_pixels + 2 * SPACE_PIXELS < MAX_DESP_HEIGHT_PIXELS) {
		CGFloat ratio = (CGFloat)(MAX_DESP_HEIGHT_PIXELS - 2 * SPACE_PIXELS) / (CGFloat)(y_pixels + z_pixels);
		x_pixels *= ratio;
		y_pixels *= ratio;
		z_pixels *= ratio;
	}
	
	if (x_pixels + z_pixels + 2 * SPACE_PIXELS > MAX_DESP_WIDTH_PIXELS) {
		CGFloat ratio = (CGFloat)(MAX_DESP_WIDTH_PIXELS - 2 * SPACE_PIXELS) / (CGFloat)(x_pixels + z_pixels);
		x_pixels *= ratio;
		y_pixels *= ratio;
		z_pixels *= ratio;
	}
	if (y_pixels + z_pixels + 2 * SPACE_PIXELS > MAX_DESP_HEIGHT_PIXELS) {
		CGFloat ratio = (CGFloat)(MAX_DESP_HEIGHT_PIXELS - 2 * SPACE_PIXELS) / (CGFloat)(y_pixels + z_pixels);
		x_pixels *= ratio;
		y_pixels *= ratio;
		z_pixels *= ratio;
	}
	

	CGRect xyBounds, xyFrame;
	xyBounds.origin.x = 0;
	xyBounds.origin.y = 0;
	xyBounds.size.width = x_pixels;
	xyBounds.size.height = y_pixels;
	//xyFrame = [self.xy_view frame];
	xyFrame.origin.x = SPACE_PIXELS;
	xyFrame.origin.y = SPACE_PIXELS;
	xyFrame.size = xyBounds.size;
	[self.xy_view setFrame:xyFrame];
	[self.xy_view setBounds:xyBounds];
	
	CGRect xzBounds, xzFrame;
	xzBounds.origin.x = 0;
	xzBounds.origin.y = 0;
	xzBounds.size.width = x_pixels;
	xzBounds.size.height = z_pixels;
	//xzFrame = [self.xz_view frame];
	xzFrame.origin.x = SPACE_PIXELS;
	xzFrame.origin.y = y_pixels + 2 * SPACE_PIXELS;
	xzFrame.size = xzBounds.size;
	[self.xz_view setFrame:xzFrame];
	[self.xz_view setBounds:xzBounds];
	
	CGRect yzBounds, yzFrame;
	yzBounds.origin.x = 0;
	yzBounds.origin.y = 0;
	yzBounds.size.width = z_pixels;
	yzBounds.size.height = y_pixels;
	//yzFrame = [self.yz_view frame];
	yzFrame.origin.x = x_pixels + 2 * SPACE_PIXELS;
	yzFrame.origin.y = SPACE_PIXELS;
	yzFrame.size = yzBounds.size;
	[self.yz_view setFrame:yzFrame];
	[self.yz_view setBounds:yzBounds];
	
	//[self.xy_view updateView];
//	[self.yz_view updateView];
//	[self.xz_view updateView];
	
	[self.xy_view setNeedsDisplay];
	[self.yz_view setNeedsDisplay];
	[self.xz_view setNeedsDisplay];
}

- (void)updateViewsData {
	xy_view.my4DImage = self.my4DImage;
	xy_view.mainViewData = self.mainViewData;
	yz_view.my4DImage = self.my4DImage;
	yz_view.mainViewData = self.mainViewData;
	xz_view.my4DImage = self.my4DImage;
	xz_view.mainViewData = self.mainViewData;
	[self.xy_view updateView];
	[self.yz_view updateView];
	[self.xz_view updateView];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
	{
		CGRect zframe, z_sliderframe;
		zframe.origin.x = MAX_DESP_WIDTH_PIXELS + SPACE_PIXELS;
		zframe.origin.y = SPACE_PIXELS;
		zframe.size.width = 40;
		zframe.size.height = 20;
		[z setFrame:zframe];
		z_sliderframe.origin.x = MAX_DESP_WIDTH_PIXELS + SPACE_PIXELS;
		z_sliderframe.origin.y = zframe.origin.y + zframe.size.height + SPACE_PIXELS;
		z_sliderframe.size.width = 180;
		z_sliderframe.size.height = 25;
		[z_slider setFrame:z_sliderframe];
		
		CGRect xframe, x_sliderframe;
		xframe.origin.x = MAX_DESP_WIDTH_PIXELS + SPACE_PIXELS;
		xframe.origin.y = z_sliderframe.origin.y + z_sliderframe.size.height + SPACE_PIXELS;
		xframe.size.width = 40;
		xframe.size.height = 20;
		[x setFrame:xframe];
		x_sliderframe.origin.x = MAX_DESP_WIDTH_PIXELS + SPACE_PIXELS;
		x_sliderframe.origin.y = xframe.origin.y + xframe.size.height + SPACE_PIXELS;
		x_sliderframe.size.width = 180;
		x_sliderframe.size.height = 25;
		[x_slider setFrame:x_sliderframe];
		
		CGRect yframe, y_sliderframe;
		yframe.origin.x = MAX_DESP_WIDTH_PIXELS + SPACE_PIXELS;
		yframe.origin.y = x_sliderframe.origin.y + x_sliderframe.size.height + SPACE_PIXELS;
		yframe.size.width = 40;
		yframe.size.height = 20;
		[y setFrame:yframe];
		y_sliderframe.origin.x = MAX_DESP_WIDTH_PIXELS + SPACE_PIXELS;
		y_sliderframe.origin.y = yframe.origin.y + yframe.size.height + SPACE_PIXELS;
		y_sliderframe.size.width = 180;
		y_sliderframe.size.height = 25;
		[y_slider setFrame:y_sliderframe];
		
		CGRect openfileframe;
		openfileframe.origin.x = MAX_DESP_WIDTH_PIXELS + 2*SPACE_PIXELS;
		openfileframe.origin.y = y_sliderframe.origin.y + y_sliderframe.size.height + 8 * SPACE_PIXELS;
		openfileframe.size.width = 120;
		openfileframe.size.height = 30;
		[openfile setFrame:openfileframe];
		
		CGRect seeIn3dframe;
		seeIn3dframe.origin.x = MAX_DESP_WIDTH_PIXELS + 2*SPACE_PIXELS;
		seeIn3dframe.origin.y = openfileframe.origin.y + openfileframe.size.height + 2 * SPACE_PIXELS;
		seeIn3dframe.size.width = 120;
		seeIn3dframe.size.height = 30;
		[seeIn3d setFrame:seeIn3dframe];
	}
	
	else if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
	{
		CGRect openfileframe;
		openfileframe.origin.x = 117;
		openfileframe.origin.y = 765;
		openfileframe.size.width = 120;
		openfileframe.size.height = 30;
		[openfile setFrame:openfileframe];
		
		CGRect seeIn3dframe;
		seeIn3dframe.origin.x = 117;
		seeIn3dframe.origin.y = 836;
		seeIn3dframe.size.width = 120;
		seeIn3dframe.size.height = 30;
		[seeIn3d setFrame:seeIn3dframe];
		
		CGRect zframe, z_sliderframe;
		zframe.origin.x = 392;
		zframe.origin.y = 751;
		zframe.size.width = 40;
		zframe.size.height = 20;
		[z setFrame:zframe];
		z_sliderframe.origin.x = 456;
		z_sliderframe.origin.y = 750;
		z_sliderframe.size.width = 180;
		z_sliderframe.size.height = 25;
		[z_slider setFrame:z_sliderframe];
		
		CGRect xframe, x_sliderframe;
		xframe.origin.x = 392;
		xframe.origin.y = 808;
		xframe.size.width = 40;
		xframe.size.height = 20;
		[x setFrame:xframe];
		x_sliderframe.origin.x = 456;
		x_sliderframe.origin.y = 807;
		x_sliderframe.size.width = 180;
		x_sliderframe.size.height = 25;
		[x_slider setFrame:x_sliderframe];
		
		CGRect yframe, y_sliderframe;
		yframe.origin.x = 392;
		yframe.origin.y = 861;
		yframe.size.width = 40;
		yframe.size.height = 20;
		[y setFrame:yframe];
		y_sliderframe.origin.x = 456;
		y_sliderframe.origin.y = 860;
		y_sliderframe.size.width = 180;
		y_sliderframe.size.height = 25;
		[y_slider setFrame:y_sliderframe];
		
	}
}
@end
